package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.contract.bean.SelectResourseBean;
import com.wwwarehouse.contract.common.ContractCommon;

import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/24 19:52
 * @描述 ${TODO}
 */

public class LocalPublishAdapter extends BaseAdapter {

    private Context                           mContext;
    private List<SelectResourseBean.ListBean> mCurrentList;
    private int                               mHeight;
    public LocalPublishAdapter(Context context, List<SelectResourseBean.ListBean> list, int height) {
        mContext = context;
        mCurrentList = list;
        mHeight = height;
    }

    @Override
    public int getCount() {
        return mCurrentList == null ? 0 : mCurrentList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCurrentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final SelectResourseBean.ListBean listBean = mCurrentList.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = parent.inflate(mContext, R.layout.can_see_publish_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivGood = (ImageView) convertView.findViewById(R.id.iv_good);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tvDel = (TextView) convertView.findViewById(R.id.tv_del);
            convertView.setTag(viewHolder);
            AbsListView.LayoutParams params =  new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mHeight;
            convertView.setLayoutParams(params);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        BaseApplication.getApplicationInstance().displayImg(mCurrentList.get(position).getRelatedUrl(), viewHolder.ivGood);
        viewHolder.tvName.setText(mCurrentList.get(position).getDefinedName());
        viewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentList.remove(listBean);
                ContractCommon.getInstance().getForLocalPublishList().remove(listBean);
                ContractCommon.getInstance().getSelectToCanSeePublishList().remove(listBean);
                EventBus.getDefault().post("ForLocalPublishListSize");
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    static class ViewHolder{
        private ImageView ivGood;
        private TextView tvName;
        private TextView tvDel;
    }
}
